//-------------------------------------------------------------------
// 头文件
//-------------------------------------------------------------------

#include "ui/screens.h"
#include "my_screens.h"
//-------------------------------------------------------------------
// 全局变量
//-------------------------------------------------------------------

lv_font_t *SourceHanSansSC_Bold_20 = NULL;

//-------------------------------------------------------------------
// 私有函数
//-------------------------------------------------------------------

static void init_font(void)
{
    //  初始化字体
    SourceHanSansSC_Bold_20 = lv_freetype_font_create(FONT_PATH,
                                                      LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
                                                      20,
                                                      LV_FREETYPE_FONT_STYLE_NORMAL); // 加载字库文件
    lv_obj_set_style_text_font(objects.film_details_label, SourceHanSansSC_Bold_20, 0);
    lv_obj_set_style_text_font(objects.member_center_discounts_label, SourceHanSansSC_Bold_20, 0);
    lv_obj_set_style_text_font(objects.member_center_account_info_label, SourceHanSansSC_Bold_20, 0);
    lv_obj_set_style_text_font(lv_dropdown_get_list(objects.member_center_dropdown), SourceHanSansSC_Bold_20, 0);
}
static void play_boot_video(void)
{
    /*.mp4 is downloaded from http://www.videezy.com (Free Stock Footage by Videezy!)*/
    lv_obj_t *player = lv_ffmpeg_player_create(objects.boot_screen);
    lv_obj_set_size(player, LV_PCT(100), LV_PCT(100));
    lv_obj_center(player);

    lv_ffmpeg_player_set_src(player, "./res/boot_video.mp4");
    lv_ffmpeg_player_set_auto_restart(player, true);
    lv_ffmpeg_player_set_cmd(player, LV_FFMPEG_PLAYER_CMD_START);

    currentScreen = SCREEN_ID_MAIN_SCREEN - 1;

    // 开机视频，模拟器上意思一下，开发板上正常时长
#if LV_USE_SDL
    lv_scr_load_anim(objects.main_screen, LV_SCR_LOAD_ANIM_FADE_IN, 200, 1 * 1000, true);
#elif LV_USE_LINUX_FBDEV
    lv_scr_load_anim(objects.main_screen, LV_SCR_LOAD_ANIM_FADE_IN, 200, 8 * 1000, true);
#else
    lv_scr_load_anim(objects.main_screen, LV_SCR_LOAD_ANIM_FADE_IN, 200, 0, true);
#endif
}

#if LV_BUILD_EXAMPLES && LV_USE_FLEX

/**
 * Show an example to scroll snap
 */
static void slideshow_init(void)
{

    lv_obj_t *panel = objects.main_screen_slideshow;
    lv_obj_set_size(panel, LV_PCT(100), LV_PCT(100));
    lv_obj_set_scroll_snap_x(panel, LV_SCROLL_SNAP_CENTER);
    lv_obj_set_flex_flow(panel, LV_FLEX_FLOW_ROW);
    lv_obj_center(panel);

    lv_obj_add_flag(panel, LV_OBJ_FLAG_SCROLL_ONE); // 一次只能滚动一个

    uint32_t i;
    for (i = 0; i < 10; i++)
    {
        lv_obj_t *btn = lv_button_create(panel);
        lv_obj_set_size(btn, LV_PCT(70), LV_PCT(100));
        lv_obj_set_style_clip_corner(btn, true, 0);
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x00000000), 0);

        lv_obj_t *label = lv_label_create(btn);
        lv_obj_t *img = lv_img_create(btn);
        switch (i)
        {
        case 5:
            lv_img_set_src(img, "./res/cd1.jpg");
            break;

        default:
            break;
        }

        lv_label_set_text_fmt(label, "advertising position %" LV_PRIu32, i);
        if (i % 2 == 0)
        {
            lv_obj_remove_flag(btn, LV_OBJ_FLAG_SNAPPABLE);
        }

        lv_obj_center(img);
        lv_obj_center(label);
    }
    lv_obj_update_snap(panel, LV_ANIM_ON);
}
#endif

static void init_ticket_screen_list()
{
    // 父组件
    lv_obj_t *parent = objects.buy_ticket_screen_mid_panel;

    // 检查父组件是否为空
    if (parent == NULL)
    {
        printf("<%s>[%d] 父组件为空\n", __FILE__, __LINE__);
        return;
    }

    // 检查电影链表是否为空
    if (list_empty(film_dcll_head))
    {
        printf("<%s>[%d] 电影链表为空\n", __FILE__, __LINE__);
        return;
    }

    // 循环遍历电影链表，创建按钮
    film_dcll *pos;
    list_for_each_entry(pos, film_dcll_head, list)
    {
        // 判断数据内容是否为空
        if (pos->film_info == NULL)
        {
            printf("<%s>[%d] 电影数据为空\n", __FILE__, __LINE__);
            continue;
        }

        lv_obj_t *btn = lv_btn_create(parent);
        if (btn == NULL)
        {
            printf("<%s>[%d] 创建按钮失败\n", __FILE__, __LINE__);
            continue;
        }
        lv_obj_t *img = lv_img_create(btn);

        lv_obj_set_size(btn, small_pic_width, small_pic_height);
        lv_obj_center(img);
        lv_img_set_src(img, pos->film_info->film_small_photo);
        lv_obj_set_style_clip_corner(btn, true, 0);

        // 添加事件回调
        lv_obj_add_event_cb(btn, action_click_the_film_for_details, LV_EVENT_CLICKED, pos->film_info);
    }
}

//-------------------------------------------------------------------
// 函数实现
//-------------------------------------------------------------------
void my_ui_init()
{
    init_font(); // 初始化字体风格

    play_boot_video(); // 播放开机动画

    slideshow_init(); // 初始化轮播框

    init_ticket_screen_list(); // 初始化买票界面
}

void init_session_list(long film_id)
{
    // 父组件
    lv_obj_t *parent = objects.film_sessions_panel;

    // 检查父组件是否为空
    if (parent == NULL)
    {
        printf("<%s>[%d] 父组件为空\n", __FILE__, __LINE__);
        return;
    }

    // 检查场次链表是否为空
    if (list_empty(session_dcll_head))
    {
        printf("<%s>[%d] 场次链表为空\n", __FILE__, __LINE__);
        return;
    }

    // 如果父组件有孩子，则清空所有孩子
    lv_obj_clean(parent);

    // 循环遍历场次链表，创建按钮
    session_dcll *pos;
    list_for_each_entry(pos, session_dcll_head, list)
    {
        // 判断数据内容是否为空
        if (pos->session_info == NULL)
        {
            printf("<%s>[%d] 场次数据为空\n", __FILE__, __LINE__);
            continue;
        }
        // 判断是否为当前电影的场次
        if (pos->session_info->film_id == film_id)
        {
            lv_obj_t *btn = lv_btn_create(parent);
            if (btn == NULL)
            {
                printf("<%s>[%d] 创建按钮失败\n", __FILE__, __LINE__);
                continue;
            }

            lv_obj_t *label = lv_label_create(btn);
            if (label == NULL)
            {
                printf("<%s>[%d] 创建标签失败\n", __FILE__, __LINE__);
                continue;
            }

            lv_obj_set_style_text_font(label, SourceHanSansSC_Bold_20, 0);

            hall_dcll *hall_pos;
            list_for_each_entry(hall_pos, hall_dcll_head, list)
            {
                if (hall_pos->hall_info->hall_id == pos->session_info->hall_id)
                {
                    lv_label_set_text(label, hall_pos->hall_info->hall_name);
                    break;
                }
            }

            lv_obj_center(label);
            lv_obj_set_style_clip_corner(btn, true, 0);

            // 添加事件回调
            lv_obj_add_event_cb(btn, action_click_the_session_to_pay, LV_EVENT_CLICKED, pos->session_info);
        }
    }
}
